starter.controller('subwayController',['$rootScope', '$window','$state','$ionicPopup','$ionicHistory','$timeout','$ionicScrollDelegate','$scope','Page','localStore','settings','$interval',
		function($rootScope, $window, $state, $ionicPopup, $ionicHistory,$timeout, $ionicScrollDelegate, $scope, Page, localStore,settings,$interval) {
	// 定位
	var map, geolocation;
	$scope.status = 1;
    // 加载地图，调用浏览器定位服务
    map = new AMap.Map('container', {
        resizeEnable: true
    });
    map.plugin('AMap.Geolocation', function() {
        geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,// 是否使用高精度定位，默认:true
            timeout: 3000,          // 超过3秒后停止定位，默认：无穷大
            buttonOffset: new AMap.Pixel(10, 20),// 定位按钮与设置的停靠位置的偏移量，默认：Pixel(10,
													// 20)
            zoomToAccuracy: true,      // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            buttonPosition:'RB'
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);// 返回定位信息
        AMap.event.addListener(geolocation, 'error', onError);      // 返回定位出错信息
    });
    // 解析定位结果
    function onComplete(data) {// 定位成功
    	$scope.status = data.status;
        $scope.longitude =  data.position.getLng();
        $scope.latitude =  data.position.getLat();
    }
    // 解析定位错误信息
    function onError(data) {
    	$scope.status = data.status;
    }
	
	window.cbk = function() {
        var mySubway = subway("mybox", {
            adcode: 4201,// 武汉市对应的adcode 高德地图城市编码对照表要去掉后两位
            theme: "colorful",
            client: 0,
            doubleclick: {
                switch: true
            }
        });
        
        // 地铁加载完成，执行complete事件
        mySubway.event.on("subway.complete", function(ev, info) {
            var id = info.status;
            // 在站点之上添加marker
            var nearStation ;// 最近地铁站
           
    	    $scope.inputTimer = $interval(function(){
    		   if($scope.longitude==null || $scope.latitude==null){// 没有获取到经纬度
    		      //定位失败判断
    			   if($scope.status == 0){
    				   alert('定位失败');
    				   $interval.cancel($scope.inputTimer); // 终止函数执行
    			   }
    		   }else {// 定位成功
           		$interval.cancel($scope.inputTimer); // 终止函数执行
           		nearStation = mySubway.getNearStation({lnglat: ''+$scope.longitude+','+$scope.latitude+''})// 离我最近的站点id 
	           	
           		if(!nearStation){// 没有获取到最近地铁站  仅测试使用
	     	    	var lng='114.2518240314';
	     	    	var lat='30.5391317847';
	     	    	nearStation = mySubway.getNearStation({lnglat: ''+lng+','+lat+''});// 离我最近的站点id 
	           	 }
     		    
           		mySubway.addMarker(nearStation,ev);
     		    var center =mySubway.getStCenter(nearStation);
     		    mySubway.setCenter(center);
     		    $("#marker-"+nearStation).find('div').css('background', 'url(img/near.png)').css('background-size','100% 100%').css('height','35px').css('width','85px').css('margin','2px 0px 0px -30px');
              }
  			 }, 500);// 时间显示0.5秒钟执行一次
        });
        
       

       
        /*  // 点击站点，显示此站点的信息窗体
	        mySubway.event.on("station.touch", function(ev, info){
	          var id = info.id;
    		  mySubway.stopAnimation();
			  mySubway.addInfoWindow(id, {});
			  var center =mySubway.getStCenter(id);
			  mySubway.setCenter(center);
			  });
		  
		 
			 * //点击线路名，高亮此线路 mySubway.event.on("lineName.touch", function(ev,
			 * info) { mySubway.showLine(info.id); //var select_obj =
			 * $('#g-select'); var select_obj =
			 * document.getElementById("g-select");
			 * mySubway.setFitView(select_obj); var center =
			 * mySubway.getSelectedLineCenter(); mySubway.setCenter(center); });
			 * 
			 * //点击空白, 关闭infowindow mySubway.event.on("subway.touch", function() {
			 * mySubway.clearInfoWindow(); });
			 * 
			 * //设置起点 mySubway.event.on("startStation.touch", function(ev, info) {
			 * mySubway.stopAnimation(); mySubway.clearInfoWindow();
			 * mySubway.setStart(info.id, {}); startInfo = info; route(); });
			 * 
			 * //设置终点 mySubway.event.on("endStation.touch", function(ev, info) {
			 * mySubway.stopAnimation(); mySubway.clearInfoWindow();
			 * mySubway.setEnd(info.id, {}); endInfo = info; route(); });
			 * 
			 * //路线规划 var startInfo = {}, endInfo = {}; function route() { if
			 * (startInfo.id && endInfo.id) { mySubway.route(startInfo.id,
			 * endInfo.id, {}); startInfo = {}; endInfo = {}; } }
			 */
    };		
		} ]);
